<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">

<head th:include="common">
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="modal fade" id="myAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    新增修改页面
                </h4>
            </div>
            <div class="modal-body">
                <form id="addForm" >
                    <input type="hidden" class="form-control" name="inforId" id="inforId">
                    <div class="input-group">
                        <span class="input-group-addon">创建时间</span>
                        <input type="date" class="form-control" name="createDate" id="createDate">
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon">消息名称</span>
                        <input type="text" class="form-control" name="infoName" id="infoName">
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon">消息描述</span>
                       <!-- <input type="text" class="form-control" name="infoName" id="infoName">-->
                        <textarea class="form-control" name="messageDescribe" id="messageDescribe" style="min-height: 50px;min-width: 70px;max-height: 50px; max-width: 70px;">

                        </textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="addOrUpdate()">
                    提交
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<button class="btn btn-primary btn-lg"   data-toggle="modal" data-target="#myAddModal">新增</button>
<table id="messageTable"></table>
</body>
<script>

    function addOrUpdate(){
        $.ajax({
            url:"messageAdmin/addOrUpdate",
            data:$("#addForm").serialize(),
            success:function (data){
                alert(data);
                queryMap();
                $("#myAddModal").modal("hidden");
            },
            error:function (){
                alert("失败");
            }
        })
    }

    $('#myAddModal').on('hidden.bs.modal', function () {

        document.querySelector('#addForm').reset();

    });
    $(function(){
        queryMap();
    })
    function queryMap(){
        $("#messageTable").bootstrapTable({
            url:"messageAdmin/queryMessageMap",
            columns:[
                {field:"messageCheckbox",checkbox:true},
                {field:"inforId",title:"消息列表ID"},
                {field:"createDate",title:"创建时间"},
                {field:"infoName",title:"消息名称"},
                {field:"messageDescribe",title:"消息描述"},
                {field:"caoz",title:"操作",
                    formatter:function(value,row){
                            return 	'<button class="btn btn-primary btn-lg"  onclick="delOneMessage('+row.inforId+')">删除</button>'+
                                '<button class="btn btn-danger btn-lg" onclick="updateMessage('+row.inforId+')" >编辑</button>'
                    }
                }
            ],
            pagination:true,
            pageSize:2,
            pageList:[2,10],
            sidePagination:'server',
        })
    }

    /**
     *
     * @param inforId
     */
    function delOneMessage(inforId){
        $.ajax({
            url: "messageAdmin/delOneMessage",
            data:{inforId:inforId},
            success:function (){
                alert("删除成功");
                queryMap();
            },
            error:function (){
                alert("删除失败");
            }
        })
    }

    /**
     *
     */
    function updateMessage(inforId){
        $.ajax({
            url:"messageAdmin/updateSelMessage",
            data:{inforId:inforId},
            dataType:"json",
            success:function (data){
                $("#myAddModal").modal("show")
                for(var i in data){
                    $("#addForm input").each(function(index,dom){
                        if (i==$(dom).attr("name")) {
                            $(dom).val(data[i])
                        }
                    })
                }
            }
        })
    }
</script>
</html>